import { Api } from './../../../providers/api-service';
import { Component } from '@angular/core';
import {  NavController, NavParams } from 'ionic-angular';
import { UrlService } from '../../../providers/url-service';

/**
 * 邀请战友
 */
@Component({
  selector: 'page-invitation-friend-home',
  templateUrl: 'invitation-friend-home.html',
})
export class InvitationFriendHomePage {
  isIonInfin = true;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public api:Api,
    public urlService: UrlService
  ) {
  }
  /**
   * 当页面加载的时候触发
   */
  ionViewDidLoad() {
    this.isIonInfin = true;
    console.log('邀请战友 InvitationFriendHomePage');
    this.GetMyfriendList(this.pageIndex);
  }

  /**
   * 离开页面时触发
   */
  ionViewDidLeave() {
    console.log(" 离开 邀请战友页面");

  }
  /**
 * 下拉刷新
 * @param refresher 
 */
  doRefresh(refresher) {
    this.pageIndex = 1;
    // this.doRefreshData.type = true;
    // this.doRefreshData.forbidFinite = true;
    this.ionViewDidLoad();
    setTimeout(() => {
      refresher.complete();
    }, 2000);
  }
  doInfinite(infiniteScroll) {
    console.log('Begin async operation');

    setTimeout(() => {
      this.GetMyfriendList(this.pageIndex);
      infiniteScroll.complete();
    }, 500);
  }
  ionChangeList = [];
  pageIndex = 1;
  userlist = [];
  /**
   * 获取好友
   */
  GetMyfriendList(pageIndex) {
    this.urlService.GetMyfriendList(pageIndex)
      .then(data => {
        console.log(data);
     
        //data = this.data;
        if (data.Code == 0) {
          if (pageIndex == 1) {
            this.userlist = data.Data.Items;
          } else {
            this.userlist = this.userlist.concat(data.Data.Items);
          }
          if (data.Data.Items < 50) {
            this.isIonInfin = false;
          } else {
            this.pageIndex++;
            this.isIonInfin = true;
          }
        }
      })
      .catch()
  }



  /**
   * 选择好友
   * @param item 
   * @param event 
   */
  updateCucumber(item, event) {
    if (event.checked) {
      this.ionChangeList.push(item);
    } else {
      this.ionChangeList.splice(item, 1);
    }
    console.table(this.ionChangeList);
  }
  /**
   * 完成按钮
   */
  isko() {
    this.api.writeLocalStorage("myfriendList", this.ionChangeList)
    this.navCtrl.pop();
  }


}
